package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.MarketCategoryVo;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminCategoryServiceImpl implements AdminCategoryService{
    @Override
    public List<MarketCategoryVo> list() {

        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(new MarketCategoryExample());

        Map<Integer,List<MarketCategory>> data=groupBypid(marketCategories);
        List<MarketCategoryVo> categoryL1Vos=new ArrayList<>();
        List<MarketCategory> categoryL1s=data.get(0);
        for (MarketCategory l1 : categoryL1s) {
            List<MarketCategoryVo> l2Vos=new ArrayList<>();
            List<MarketCategory> categoryL2s=data.get(l1.getId());
            if(categoryL2s!=null){
                for (MarketCategory l2 : categoryL2s) {
                    MarketCategoryVo l2vo=new MarketCategoryVo(l2.getId(),l2.getName(),l2.getKeywords(),l2.getDesc(),l2.getIconUrl(),l2.getPicUrl(),l2.getLevel(),l2.getPid(),null);
                    l2Vos.add(l2vo);
                }
            }

            MarketCategoryVo categoryL1Vo=new MarketCategoryVo(l1.getId(), l1.getName(),l1.getKeywords(), l1.getDesc(), l1.getIconUrl(), l1.getPicUrl(), l1.getLevel(),l1.getPid(),l2Vos);
            categoryL1Vos.add(categoryL1Vo);
        }
        return categoryL1Vos;
    }

    @Override
    public void create(MarketCategory marketCategory) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        marketCategory.setAddTime(LocalDateTime.now());
        marketCategory.setUpdateTime(LocalDateTime.now());
        marketCategoryMapper.insert(marketCategory);
        session.commit();
        session.close();
    }

    @Override
    public List<MarketCategory> responseL1() {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
        return marketCategories;
    }

    @Override
    public void update(MarketCategory marketCategory, Integer setId) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(setId);
        marketCategoryMapper.updateByExampleSelective(marketCategory,marketCategoryExample);
        session.commit();
        session.close();
    }

    @Override
    public void delete(Integer id, Integer deletePid) {
        SqlSession session = MybatisUtils.getSession();
        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        if(deletePid!=null){
            SqlSession sessionDeleteL1 = MybatisUtils.getSession();
            MarketCategoryMapper marketCategoryMapperDeleteL1 = sessionDeleteL1.getMapper(MarketCategoryMapper.class);
            MarketCategoryExample marketCategoryExampleDeleteL1 = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteriaDeleteL1 = marketCategoryExampleDeleteL1.createCriteria();
            criteriaDeleteL1.andPidEqualTo(deletePid);
            marketCategoryMapper.deleteByExample(marketCategoryExampleDeleteL1);
            sessionDeleteL1.commit();
            sessionDeleteL1.close();
        }
        marketCategoryMapper.deleteByExample(marketCategoryExample);
        session.commit();
        session.close();

    }


    private Map<Integer, List<MarketCategory>> groupBypid(List<MarketCategory> marketCategories) {
        Map<Integer, List<MarketCategory>> map = new HashMap<Integer, List<MarketCategory>>();
        for (MarketCategory categeory : marketCategories) {
            Integer pid=categeory.getPid();
            List<MarketCategory> list=map.get(pid);
            if(list==null){
                list=new ArrayList<MarketCategory>();
            }
            list.add(categeory);
            map.put(pid,list);
        }
        return map;
    }
}
